package com.shiwaixiangcun.customer.utils

import android.app.Activity
import android.util.Log
import android.widget.Toast
import com.google.gson.reflect.TypeToken
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.shiwaixiangcun.customer.entity.ResponseEntity
import com.shiwaixiangcun.customer.pay.AliInfo
import com.shiwaixiangcun.customer.pay.AliPay
import com.shiwaixiangcun.customer.pay.WeiXinInfo
import com.shiwaixiangcun.customer.pay.WeiXinPay

/**
 * Author   Administrator
 * Date     2018/4/12
 * Desc     eg
 */


object PropertyPayUtils {
    private val BUG_TAG = "PayUtil"


//    fun pay(payUrl: String,params: HttpParams)
    /**
     * 调用支付宝进行支付
     *
     * @param payUrl      支付路径
     * @param orderNumber 订单号
     * @param tokenString token值
     * @param mContext    调用Activity
     */
    fun payAli(payUrl: String, orderNumber: String, tokenString: String, mContext: Activity) {
        val stringBuilder = StringBuilder()
        stringBuilder.append("orderNumber=")
                .append(orderNumber)
                .append("&signkey=")
                .append("ec3c9d73689c4e24ae7e4783dda56a60")
        val sign = MD5Util.md5(stringBuilder.toString())
        OkGo.post<String>(payUrl)

                .params("payWay", "ZhiFuBao")
                .params("access_token", tokenString)
                .params("statementIds", orderNumber)
                .params("sign", sign)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        val type = object : TypeToken<ResponseEntity<AliInfo>>() {

                        }.type
                        val entity = JsonUtil.fromJson<ResponseEntity<AliInfo>>(response.body(), type)
                        if (null == entity || entity.data.zhiFuBaoResponse == null) {
                            Toast.makeText(mContext, "支付失败", Toast.LENGTH_SHORT).show()
                            return
                        }
                        val zhiFuBaoResponse = entity.data.zhiFuBaoResponse
                        AliPay.instance.pay(zhiFuBaoResponse!!, mContext)
                    }

                    override fun onError(response: Response<String>) {
                        super.onError(response)

                        val rawCall = response.rawCall
                    }
                })
    }

    /**
     * 调用微信支付
     *
     * @param orderNumber
     * @param tokenString
     * @param context
     */
    fun payWeixin(url: String, orderNumber: String, tokenString: String, context: Activity) {
        val stringBuilder = StringBuilder()
        stringBuilder.append("orderNumber=")
                .append(orderNumber)
                .append("&signkey=")
                .append("ec3c9d73689c4e24ae7e4783dda56a60")
        val sign = MD5Util.md5(stringBuilder.toString())
        OkGo.post<String>(url)
                .params("payWay", "WeiXin")
                .params("access_token", tokenString)
                .params("orderNumber", orderNumber)
                .params("sign", sign)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        val type = object : TypeToken<ResponseEntity<WeiXinInfo>>() {

                        }.type
                        val entity = JsonUtil.fromJson<ResponseEntity<WeiXinInfo>>(response.body(), type)
                                ?: return
                        val weiXinResponse = entity.data.weiXinResponse
                        val wXpay = WeiXinPay(weiXinResponse!!)
                        wXpay.createWXAPI(context)
                        wXpay.sendPayReq()

                    }

                    override fun onError(response: Response<String>) {
                        super.onError(response)

                        val rawCall = response.rawCall
                        Log.e(BUG_TAG, rawCall.request().url().toString())
                        Log.e(BUG_TAG, "微信支付    " + response.body())
                    }
                })


    }
}

